<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input
            v-model="deptName"
            placeholder="请输入部门名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            :data="deptOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            default-expand-all
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <!--用户数据-->
      <el-col :span="20" :xs="24">
        <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
          <el-form-item label="账号" prop="userName">
            <el-input
              v-model="queryParams.userName" placeholder="请输入账号"
              clearable size="small" style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="姓名" prop="nickName">
            <el-input
              v-model="queryParams.nickName" placeholder="请输入姓名"
              clearable size="small" style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="手机号码" prop="phonenumber">
            <el-input
              v-model="queryParams.phonenumber" placeholder="请输入手机号码"
              clearable size="small" style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="" prop="withSmall">
            <el-checkbox v-model="queryParams.withSmall" @change="handleQuery">包含小号</el-checkbox>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-row>

        <el-table v-loading="loading" :data="userList">
          <el-table-column label="用户编号" width="80" align="left" key="userId" prop="userId" />
          <el-table-column label="用户名称" align="left" key="userName" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              {{scope.row.userName}} <el-tag v-if="scope.row.isSmall" type="info">{{' 大号:'+scope.row.mainUserName}}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="用户昵称" align="left" key="nickName" prop="nickName" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <router-link :to="'/person/userTableForUser/' + scope.row.userId" class="link-type">
                <span>{{ scope.row.nickName }}</span>
              </router-link>
            </template>
          </el-table-column>
          <el-table-column label="部门" align="left" key="deptName" prop="dept.deptName" :show-overflow-tooltip="true" />
          <el-table-column label="手机号码" align="left" key="phonenumber" prop="phonenumber" width="150" />
          <el-table-column label="状态" align="left" key="status" width="80">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.status"
                active-value="0"
                inactive-value="1"
                @change="handleStatusChange(scope.row)"
              ></el-switch>
            </template>
          </el-table-column>
          <el-table-column label="创建时间" align="left" prop="createTime" width="160">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>
  </div>
</template>

<script>
    import {listUser} from "@/api/system/user";
    import {treeselect} from "@/api/system/dept";
    import Treeselect from "@riophae/vue-treeselect";
    import "@riophae/vue-treeselect/dist/vue-treeselect.css";
    import {listRole} from "@/api/system/role";

    export default {
        name: "User",
        components: { Treeselect },
        data() {
            return {
                // 遮罩层
                loading: true,
                // 总条数
                total: 0,
                // 用户表格数据
                userList: null,
                selectUserList: [],
                // 弹出层标题
                title: "",
                // 部门树选项
                deptOptions: undefined,
                // 是否显示弹出层
                open: false,
                // 部门名称
                deptName: undefined,
                // 默认密码
                initPassword: undefined,
                // 日期范围
                dateRange: [],
                // 状态数据字典
                statusOptions: [],
                // 性别状态字典
                sexOptions: [],
                // 岗位选项
                postOptions: [],
                // 角色选项
                roleOptions: [],
                // 表单参数
                form: {},
                defaultProps: {
                    children: "children",
                    label: "label"
                },
                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 10,
                    userName: undefined,
                    phonenumber: undefined,
                    status: undefined,
                    deptId: undefined,
                    withSmall: false
                },
            };
        },
        watch: {
            // 根据名称筛选部门树
            deptName(val) {
                this.$refs.tree.filter(val);
            }
        },
        created() {
            this.getList();
            this.getTreeselect();
            this.getDicts("sys_normal_disable").then(response => {
                this.statusOptions = response.data;
            });
            this.getDicts("sys_user_sex").then(response => {
                this.sexOptions = response.data;
            });
            this.getConfigKey("sys.user.initPassword").then(response => {
                this.initPassword = response.msg;
            });
            listRole({pageNum: 1, pageSize: 99999}).then(
                response => {
                    this.roleOptions = response.rows;
                }
            );
        },
        methods: {
            //关键字检索用户
            loadUserList(queryString) {
                this.selectUserList = [];
                if(!queryString) {
                    return;
                }
                listUser({pageNum: 1, pageSize: 20, userName: queryString}).then(
                    response => {
                        this.selectUserList = response.rows;
                    }
                );
            },
            /** 查询用户列表 */
            getList() {
                this.loading = true;
                listUser(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
                        this.userList = response.rows;
                        this.total = response.total;
                        this.loading = false;
                    }
                );
            },
            /** 查询部门下拉树结构 */
            getTreeselect() {
                treeselect().then(response => {
                    this.deptOptions = response.data;
                });
            },
            // 筛选节点
            filterNode(value, data) {
                if (!value) return true;
                return data.label.indexOf(value) !== -1;
            },
            // 节点单击事件
            handleNodeClick(data) {
                this.queryParams.deptId = data.id;
                this.getList();
            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.page = 1;
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.dateRange = [];
                this.resetForm("queryForm");
                this.handleQuery();
            },
        }
    };
</script>
